home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  9KB  |  393 lines

  1. /* output.c, output generator for dlg
  2.  *
  3.  * Will Cohen
  4.  * 10/24/92
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "dlg.h"
  9. #ifdef MEMCHK
  10. #include "trax.h"
  11. #endif
  12.  
  13. int operation_no = 0; /* used to mark nodes so that infinite loops avoided */
  14. int dfa_basep[MAX_MODES];     /* start of each group of states */
  15. int dfa_class_nop[MAX_MODES];    /* number of elements in each group of states*/
  16.  
  17. int gen_ansi = FALSE;        /* allows ansi code to be generated */
  18.  
  19. FILE *input_stream;    /* where to read description from */
  20. FILE *output_stream;    /* where to put the output      */
  21. FILE *mode_stream;    /* where to put the mode.h stuff */
  22.  
  23. /* NOTE: This section is MACHINE DEPENDENT */
  24. #define DIF_SIZE 4
  25. #ifdef PC
  26. long typesize[DIF_SIZE]  = { 0x7f, 0x7fff, 0x7fff, 0x7fffffff };
  27. char t0[] = "unsigned char";
  28. char t1[] = "unsigned short";
  29. char t2[] = "unsigned int";
  30. char t3[] = "unsigned long";
  31. char *typevar[DIF_SIZE] = { t0, t1, t2, t3};
  32. #else
  33. long typesize[DIF_SIZE]  = { 0x7f, 0x7fff, 0x7fffffff, 0x7fffffff };
  34. char t0[] = "unsigned char";
  35. char t1[] = "unsigned short";
  36. char t2[] = "unsigned int";
  37. char t3[] = "unsigned long";
  38. char *typevar[DIF_SIZE] = { t0, t1, t2, t3};
  39. #endif
  40.  
  41. /* generate require header on output */
  42. p_head()
  43. {
  44.     fprintf(OUT, "/*\n");
  45.     fprintf(OUT, " * D L G tables\n");
  46.     fprintf(OUT, " *\n");
  47.     fprintf(OUT, " * Generated from:");
  48.     fprintf(OUT, " %s", file_str[0]);
  49.     fprintf(OUT, "\n");
  50.     fprintf(OUT, " *\n");
  51.     fprintf(OUT, " * 1989-1992 by  Will Cohen, Terence Parr, and Hank Dietz\n");
  52.     fprintf(OUT, " * Purdue University Electrical Engineering\n");
  53.     fprintf(OUT, " * DLG Version %s\n", version);
  54.     fprintf(OUT, " */\n\n");
  55.     fprintf(OUT, "#include \"%s\"\n\n", mode_file);
  56.     fprintf(OUT,"\n");
  57. }
  58.  
  59.  
  60. /* generate code to tie up any loose ends */
  61. p_tail()
  62. {
  63.     fprintf(OUT, "\n");
  64.     fprintf(OUT, "\n");
  65.     if (comp_level)
  66.         fprintf(OUT, "#define ZZSHIFT(c) (b_class_no[zzauto][1+c])\n");
  67.     else
  68.         fprintf(OUT, "#define ZZSHIFT(c) (1+c)\n");
  69.     fprintf(OUT, "#define MAX_MODE %d\n",mode_counter);
  70.     fprintf(OUT, "#include \"dlgauto.h\"\n");
  71. }
  72.  
  73.  
  74. /* output the table of DFA for general use */
  75. p_tables()
  76. {
  77.     char *minsize();
  78.  
  79.     fprintf(OUT, "#define DfaStates\t%d\n", dfa_allocated);
  80.     fprintf(OUT, "typedef %s DfaState;\n\n", minsize(dfa_allocated));
  81.  
  82.     p_node_table();
  83.     p_dfa_table();
  84.     p_accept_table();
  85.     p_action_table();
  86.     p_base_table();
  87.     p_class_table();
  88.     if (comp_level)
  89.         p_bshift_table();
  90.     if (interactive)
  91.         p_alternative_table();
  92. }
  93.  
  94.  
  95. /* figures out the smallest variable type that will hold the transitions
  96.  */
  97. char *minsize(elements)
  98. int elements;
  99. {
  100.     int i = 0;
  101.  
  102.     while (elements > typesize[i])
  103.         ++i;
  104.     return typevar[i];
  105.         
  106. }
  107.  
  108.  
  109. p_node_table()
  110. {
  111.     register int    i;
  112.     register int    m = 0;
  113.  
  114.     for(m=0; m<(mode_counter-1); ++m){
  115.         for(i=dfa_basep[m]; i<dfa_basep[m+1]; ++i)
  116.             p_single_node(i,dfa_class_nop[m]);
  117.     }
  118.     for(i=dfa_basep[m]; i<=dfa_allocated; ++i)
  119.         p_single_node(i,dfa_class_nop[m]);
  120. }
  121.  
  122.  
  123. p_single_node(i,classes)
  124. int i,classes;
  125. {
  126.     register int    j;
  127.     register int    trans, items_on_line;
  128.  
  129.     fprintf(OUT, "static DfaState st%d[%d] = {\n  ", (i-1), (classes+1));
  130.     items_on_line = MAX_ON_LINE;
  131.     for(j=0; j<classes; ++j){
  132.         trans = DFA(i)->trans[j];
  133.         if (trans == NIL_INDEX)
  134.             trans = dfa_allocated+1;
  135.         /* all of DFA moved down one in array */
  136.         fprintf(OUT, "%d", trans-1);
  137.         fprintf(OUT, ", ");
  138.         if (!(--items_on_line)){
  139.             fprintf(OUT, "\n  ");
  140.             items_on_line = MAX_ON_LINE;
  141.         }
  142.     }
  143.     fprintf(OUT, "%d\n};\n\n", dfa_allocated);
  144. }
  145.  
  146.  
  147. p_dfa_table()
  148. {
  149.     register int    i;
  150.  
  151.     fprintf(OUT, "\nDfaState *dfa[%d] = {\n", dfa_allocated);
  152.     for (i=0; i<(dfa_allocated-1); ++i){
  153.         fprintf(OUT, "\tst%d,\n", i);
  154.     }
  155.     fprintf(OUT, "\tst%d\n", i);
  156.     fprintf(OUT, "};\n\n");
  157. }
  158.  
  159.  
  160. p_accept_table()
  161. {
  162.     register int    i = 1;
  163.     register int    items_on_line = 0;
  164.     int        true_interactive = TRUE;
  165.  
  166.     fprintf(OUT,"\nDfaState accepts[%d] = {\n  ",dfa_allocated);
  167.     while (TRUE){
  168.         int accept;
  169.         set nfa_states;
  170.         unsigned int *t, *nfa_i;
  171.  
  172.         nfa_states = DFA(i)->nfa_states;
  173.         t = nfa_i = set_pdq(nfa_states);
  174.         /* NOTE: picks lowest accept because accepts monotonic    */
  175.         /*    with respect to nfa node numbers and set_pdq    */
  176.         /*    returns in that order                */
  177.         while((*nfa_i != nil) && (!(accept = NFA(*nfa_i)->accept))){
  178.             nfa_i++;
  179.         }
  180.         if ((DFA(i)->alternatives) && (accept != 0)){
  181.             true_interactive = FALSE;
  182.         }
  183.         fprintf(OUT, "%d", accept);
  184.         if ((++i)>dfa_allocated)
  185.             break;
  186.         fprintf(OUT, ", ");
  187.         if ((++items_on_line)>=MAX_ON_LINE){
  188.             fprintf(OUT,"\n  ");
  189.             items_on_line = 0;
  190.         }
  191.         free(t);
  192.     }
  193.     fprintf(OUT, "\n};\n\n");
  194. }
  195.  
  196.  
  197. p_action_table()
  198. {
  199.     register int    i;
  200.  
  201.     fprintf(OUT, "void (*actions[%d])() = {\n", action_no+1);
  202.     fprintf(OUT, "\tzzerraction,\n");
  203.     for (i=1; i<action_no; ++i){
  204.         fprintf(OUT,"\tact%d,\n", i);
  205.         }
  206.     fprintf(OUT, "\tact%d\n", i);
  207.     fprintf(OUT, "};\n\n");
  208. }
  209.  
  210.  
  211. p_shift_table(m)
  212. int m;
  213. {
  214.     register int    i = 0, j;
  215.     register int    items_on_line = 0;
  216.  
  217.     fprintf(OUT, "unsigned char shift%d[%d] = {\n  ", m, MAX_CHAR+2);
  218.     while (TRUE){
  219.         /* find which partition character i is in */
  220.         for (j=0; j<dfa_class_nop[mode_counter]; ++j){
  221.             if (set_el(i,class[j]))
  222.                 break;
  223.             }
  224.         fprintf(OUT,"%d",j);
  225.         if ((++i)>(MAX_CHAR+1))
  226.             break;
  227.         fprintf(OUT,", ");
  228.         if ((++items_on_line)>=MAX_ON_LINE){
  229.             fprintf(OUT,"\n  ");
  230.             items_on_line = 0;
  231.             }
  232.         }
  233.     fprintf(OUT, "\n};\n\n");
  234. }
  235.  
  236.  
  237. p_base_table()
  238. {
  239.     register int m;
  240.  
  241.     fprintf(OUT, "static int dfa_base[] = {\n");
  242.     for(m=0; m<(mode_counter-1); ++m)
  243.         fprintf(OUT, "\t%d,\n", dfa_basep[m]-1);
  244.     fprintf(OUT, "\t%d\n};\n\n", dfa_basep[m]-1);
  245. }
  246.  
  247.  
  248. p_class_table()
  249. {
  250.     register int m;
  251.  
  252.     fprintf(OUT,"static int dfa_class_no[] = {\n");
  253.     for(m=0; m<(mode_counter-1); ++m)
  254.         fprintf(OUT,"\t%d,\n", dfa_class_nop[m]);
  255.     fprintf(OUT,"\t%d\n};\n\n", dfa_class_nop[m]);
  256. }
  257.  
  258.  
  259. p_bshift_table()
  260. {
  261.     register int m;
  262.  
  263.     fprintf(OUT,"static unsigned char *b_class_no[] = {\n");
  264.     for(m=0; m<(mode_counter-1); ++m)
  265.         fprintf(OUT, "\tshift%d,\n", m);
  266.     fprintf(OUT, "\tshift%d\n};\n\n", m);
  267. }
  268.  
  269. p_alternative_table()
  270. {
  271.     register int i;
  272.  
  273.     fprintf(OUT, "#define ZZINTERACTIVE\n\n");
  274.     fprintf(OUT, "char zzalternatives[DfaStates+1] = {\n");
  275.     for(i=1; i<=dfa_allocated; ++i)
  276.         fprintf(OUT, "\t%d,\n", DFA(i)->alternatives);
  277.     fprintf(OUT, "/* must have 0 for zzalternatives[DfaStates] */\n");
  278.     fprintf(OUT, "\t0\n};\n\n");
  279. }
  280.  
  281. p_mode_def(s,m)
  282. char *s;
  283. int m;
  284. {
  285.     fprintf(mode_stream, "#define %s %d\n", s, m);
  286. }
  287.  
  288.  
  289. #ifdef DEBUG
  290. /* print out a particular nfa node that is pointed to by p */
  291. p_nfa_node(p)
  292. nfa_node *p;
  293. {
  294.      register nfa_node *t;
  295.  
  296.     if (p != NIL_INDEX){
  297.         printf("NFA state : %d\naccept state : %d\n",
  298.             NFA_NO(p),p->accept);
  299.         if (p->trans[0] != NIL_INDEX){
  300.             printf("trans[0] => %d on ", NFA_NO(p->trans[0]));
  301.             p_set(p->label);
  302.             printf("\n");
  303.             }
  304.         else
  305.             printf("trans[0] => nil\n");
  306.         if (p->trans[1] != NIL_INDEX)
  307.             printf("trans[1] => %d on epsilon\n",
  308.                 NFA_NO(p->trans[1]));
  309.         else
  310.             printf("trans[1] => nil\n");
  311.         printf("\n");
  312.         }
  313. }
  314. #endif
  315.  
  316. #ifdef  DEBUG
  317. /* code to print out special structures when using a debugger */
  318.  
  319. p_nfa(p)
  320. nfa_node *p;    /* state number also index into array */
  321. {
  322. /* each node has a marker on it so it only gets printed once */
  323.  
  324.     operation_no++; /* get new number */
  325.     s_p_nfa(p);
  326. }
  327.  
  328. s_p_nfa(p)
  329. nfa_node *p;    /* state number also index into array */
  330. {
  331.     if ((p != NIL_INDEX) && (p->nfa_set != operation_no)){
  332.         /* so it is only printed once */
  333.         p->nfa_set = operation_no;
  334.         p_nfa_node(p);
  335.         s_p_nfa(p->trans[0]);
  336.         s_p_nfa(p->trans[1]);
  337.         }
  338. }
  339.  
  340. p_dfa_node(p)
  341. dfa_node *p;
  342. {
  343.     int i;
  344.  
  345.     if (p != NIL_INDEX){
  346.         printf("DFA state :%d\n",NFA_NO(p));
  347.         if (p->done)
  348.             printf("done\n");
  349.         else
  350.             printf("undone\n");
  351.         printf("from nfa states : ");
  352.         p_set(p->nfa_states);
  353.         printf("\n");
  354.         /* NOTE: trans arcs stored as ints rather than pointer*/
  355.         for (i=0; i<class_no; i++){
  356.             printf("%d ",p->trans[i]);
  357.             }
  358.         printf("\n\n");
  359.         }
  360. }
  361.  
  362. p_dfa()
  363. {
  364. /* prints out all the dfa nodes actually allocated */
  365.  
  366.     int i;
  367.  
  368.     for (i = 1; i<=dfa_allocated; i++)
  369.         p_dfa_node(NFA(i));
  370. }
  371.  
  372.  
  373. /* print out numbers in the set label */
  374. p_set(label)
  375. set label;
  376. {
  377.     unsigned *t, *e;
  378.  
  379.     if (set_nil(label)){
  380.         printf("epsilon\n");
  381.     }else{
  382.         t = e = set_pdq(label);
  383.         while(*e != nil){
  384.             printf("%d ", (*e+MIN_CHAR));
  385.             e++;
  386.         }
  387.         printf("\n");
  388.         free(t);
  389.     }
  390.     
  391. }
  392. #endif
  393.